#include "DYSvrNetMan.h"
#include "stdio.h"


DYSvrNetMan::DYSvrNetMan(void)
{

}

DYSvrNetMan::~DYSvrNetMan(void)
{
	SvrStop();
}

int DYSvrNetMan::SetConnectTime(UINT32 nWaitTime, UINT32 nTryTimes)
{
	m_xAnchorListen.SetConnectTime( nWaitTime,  nTryTimes );
	m_xViewerListen.SetConnectTime( nWaitTime,  nTryTimes );
	m_xAudienceListen.SetConnectTime( nWaitTime,  nTryTimes );

	return 0;
}

int DYSvrNetMan::SetListen(UINT16 nPort1, UINT16 nPort2, UINT16 nPort3)
{
	m_xAnchorListen.SetListen( eAnchorListen,  nPort1 );
	m_xViewerListen.SetListen( eViewerListen, nPort2 );
	m_xAudienceListen.SetListen( eAudienceListen,nPort3 );

	return 0;
}


int DYSvrNetMan::SvrStart()
{
	m_xAnchorListen.SetSvrNetMan( this );
	m_xViewerListen.SetSvrNetMan( this );
	m_xAudienceListen.SetSvrNetMan( this );

	m_xSessionMan.SetSvrNetMan(this);


	m_xAnchorListen.Start();
	m_xViewerListen.Start();
	m_xAudienceListen.Start();

	return 0;
}

int DYSvrNetMan::SvrStop()
{
	m_xAnchorListen.Stop();
	m_xViewerListen.Stop();
	m_xAudienceListen.Stop();


	m_xSessionMan.UnInitialize();


	return TG_ERR_SUCCESS;
}


int DYSvrNetMan::TcpDispatch(UINT16 nType,SOCKET hClientSocket,NET_PACKET_HEAD *pMsgHead,BYTE *pBuffer)
{
	switch( nType )
	{
	case eAnchorListen:
		{
			return TcpAnchorDispatch(hClientSocket,pMsgHead,pBuffer);
		}
		break;
	case eViewerListen:
		{
			return TcpViewerDispatch(hClientSocket,pMsgHead,pBuffer);
		}
		break;
	case eAudienceListen:
		{
			return TcpAudienceDispatch(hClientSocket,pMsgHead,pBuffer);
		}
		break;
	}
    return 0;
}

int DYSvrNetMan::UdpDispatch(UINT16 nType,SOCKET hSocket,TG_UDP_ADDR *pAddr,NET_PACKET_HEAD *pMsgHead,BYTE *pBuffer)
{
	switch( nType )
	{
	case eAnchorListen:
		{
			return UdpAnchorCommand(hSocket,pAddr,pMsgHead,pBuffer);
		}
		break;
	case eViewerListen:
		{
			return UdpViewerCommand(hSocket,pAddr,pMsgHead,pBuffer);
		}
		break;
	case eAudienceListen:
		{
			return UdpAudienceCommand(hSocket,pAddr,pMsgHead,pBuffer);
		}
		break;
	}
	return 0;
}

